package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/destination")
public class DetinationController {

    @Reference
    private IRegionService regionService;


    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;


    @RequestMapping("")
    public String index(Model model){

        model.addAttribute("hotRegions", regionService.queryHotRegions());

        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){
        List<Destination> list = destinationService.getDestByRegionId(regionId);

        List<Destination> leftDests = new ArrayList<>();
        List<Destination> rightDests = new ArrayList<>();

        if(list != null && list.size()>0){
            int mid = list.size() / 2;
            for(int i=0;i<list.size();i++){
                if(i<mid){
                    leftDests.add(list.get(i));
                }else{
                    rightDests.add(list.get(i));
                }
            }
        }
        model.addAttribute("leftDests",leftDests);
        model.addAttribute("rightDests",rightDests);
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model,Long id){
        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        model.addAttribute("toasts",toasts);

        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);

        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);

        //strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.getViewnumTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){

        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts",toasts);

        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);

        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){

        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        //catalog 选中的分类
        StrategyCatalog catalog = strategyCatalogService.getCatalogById(qo.getCatalogId());
        model.addAttribute("catalog",catalog);

        //detail  选中的分类中第一个攻略详情
        List<StrategyDetail> details = catalog.getDetails();
        StrategyDetail detail = details.get(0);
        //content 加入内容
        StrategyContent content = strategyDetailService.getContentByDetailId(detail.getId());
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);

        return "destination/surveyTpl";
    }


    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo){
        //pageInfo
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";
    }
}
